package cn.hznc.controller.toolAnalyze;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.OrderCraftEntity;
import cn.hznc.domain.entity.OrderEntity;
import cn.hznc.mapper.OrderCraftMapper;
import cn.hznc.mapper.OrderMapper;
import cn.hznc.vo.CraftParamVo;
import cn.hznc.vo.ExportOverdueOrdersVo;
import cn.hznc.vo.InCraftVo;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.util.StringUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/exportOverdueOrders")
public class ExportOverdueOrdersController {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderCraftMapper orderCraftMapper;

    @ResponseBody
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    public void getExcel(HttpServletResponse response) throws IOException {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ExportOverdueOrdersVo> exportOverdueOrdersVoList = new ArrayList<>();
        LambdaQueryWrapper<OrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(OrderEntity::getFinishTime,dateFormat.format(date));
        List<OrderEntity> orderEntityList = orderMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(orderEntityList)){
            orderEntityList.forEach(orderEntity -> {
                QueryWrapper<OrderCraftEntity> orderCraftWrapper = new QueryWrapper<>();
                orderCraftWrapper.eq("task_id", orderEntity.getTaskId());
                OrderCraftEntity orderCraftEntity = orderCraftMapper.selectOne(orderCraftWrapper);
                if (ObjectUtil.isNotNull(orderCraftEntity) && StringUtil.isNotEmpty(orderCraftEntity.getCraftParam()) && !"[]".equals(orderCraftEntity.getCraftParam())) {
                    List<CraftParamVo> craftParamVos = JSONArray.parseArray(orderCraftEntity.getCraftParam(), CraftParamVo.class).stream().
                            sorted(Comparator.comparingInt(CraftParamVo::getSeq)).
                            collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(craftParamVos)) {
                        craftParamVos.forEach(craftParamVo -> {
                            if (ObjectUtil.isNotNull(craftParamVo.getStatus()) && craftParamVo.getStatus() == 5) {
                                ExportOverdueOrdersVo exportOverdueOrdersVo = new ExportOverdueOrdersVo();
                                if (orderEntity.getAbutmentStatus() == 0){
                                    exportOverdueOrdersVo.setOrderStatus("工单未下达");
                                }else if (orderEntity.getAbutmentStatus() == 3){
                                    exportOverdueOrdersVo.setOrderStatus("未投料");

                                }else if (orderEntity.getAbutmentStatus() == 5){
                                    exportOverdueOrdersVo.setOrderStatus("在制");

                                }else if (orderEntity.getAbutmentStatus() == 9){
                                    exportOverdueOrdersVo.setOrderStatus("完成");

                                }
                                exportOverdueOrdersVo.setPartno(orderEntity.getMaterialNo());
                                exportOverdueOrdersVo.setProductionName(orderEntity.getProductionName());
                                exportOverdueOrdersVo.setProductionNum(orderEntity.getProductionNum());
                                exportOverdueOrdersVo.setFinishTime(dateFormat.format(orderEntity.getFinishTime()));
                                exportOverdueOrdersVo.setTaskId(orderEntity.getTaskId());
                                exportOverdueOrdersVo.setInCraftName(craftParamVo.getName());
                                exportOverdueOrdersVo.setPlanNum(orderEntity.getPlanNumber());
                                exportOverdueOrdersVoList.add(exportOverdueOrdersVo);
//                                InCraftVo inCraftVo = new InCraftVo();
//                                inCraftVo.setInCraftName(craftParamVo.getName());
//                                inCraftVo.setInCraftSeq(craftParamVo.getSeq());
//                                inCraftVo.setStartTime(craftParamVo.getStartTime());
//                                inCraftVo.setQualifiedNumber(craftParamVo.getQualifiedNumber());
//                                inCraftVo.setEndTime(craftParamVo.getEndTime());
//                                inCraftVo.setInboundNumber(craftParamVo.getInboundNumber());
//                                inCraftVo.setRejectedNumber(craftParamVo.getRejectedNumber());
//                                inCraftVo.setInProcessNumber(craftParamVo.getInProcessNumber());
//                                inCraftVo.setBz(craftParamVo.getBz());
//                                inCraftVos.add(inCraftVo);
                            }
                        });
                    }
                }
//                ExportOverdueOrdersVo exportOverdueOrdersVo = new ExportOverdueOrdersVo();
//                exportOverdueOrdersVo.setOrderStatus(orderEntity.getOrderStatus());
//                exportOverdueOrdersVo.setPartno(orderEntity.getMaterialNo());
//                exportOverdueOrdersVo.setProductionName(orderEntity.getProductionName());
//                exportOverdueOrdersVo.setProductionNum(orderEntity.getProductionNum());
//                exportOverdueOrdersVo.setReleaseTime(orderEntity.getReleaseTime());
//                exportOverdueOrdersVo.setTaskId(orderEntity.getTaskId());
//                exportOverdueOrdersVoList.add(exportOverdueOrdersVo);
            });
        }
        //设置返回输出流的格式
        response.setContentType("application/vnd.ms-excel");
        //设置响应头部信息，格式为附件，文件名为expert.xlsx
        response.setHeader("Content-Disposition","attachment; filename=" + "testexcel.xlsx");
        writeExcel(response,exportOverdueOrdersVoList);
    }

    public static void writeExcel(HttpServletResponse response, List<ExportOverdueOrdersVo> list) throws IOException {
        ExcelWriter excelWriter= EasyExcel.write(response.getOutputStream()).build();
        //sheet的含义相当于一张工作表
        WriteSheet sheet=EasyExcel.writerSheet(0,"sheet").head(ExportOverdueOrdersVo.class).build();
        //往excel中写入数据
        excelWriter.write(list,sheet);
        //关闭写入流
        excelWriter.finish();
    }
}
